<?php

/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Controller', 'Controller');
/**
 * Define constants for application
 */
require_once ROOT . DS . APP_DIR . DS . 'Config/consts.php';

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {

    public $components = array(
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'userModel' => 'User',
                    'fields' => array('username' => 'email', 'password' => 'password'),
                    'scope' => array('User.activate_flg' => 1, 'User.del_flg' => 0),
                )
            ),
            //'authorize' => array('Controller'),
            //'authError' => 'Did you really think you are allowed to see that?',
            'loginRedirect' => HOME_URL,
            'logoutRedirect' => HOME_URL
        ),
        'Cookie' => array(
            'name' => COOKIE_NAME,
            'domain' => COOKIE_DOMAIN,
            'time' => COOKIE_TIME,
            'key' => COOKIE_ENCRYPT_KEY
        ),
        'Session',
            //'DebugKit.Toolbar',
    );
    public $helpers = array(
        'Html',
        'Form',
        'Session',
//        'MyPlugin.Menu' => array(
//            'activeClass' => 'active',
//            'firstClass' => 'first',
//            'childrenClass' => 'has-children',
//            'itemFormat' => '<li%s>%s%s</li>',
//            'wrapperFormat' => '<ul%s>%s</ul>',
//            'noLinkFormat' => '<a href="#">%s</a>',
//            'menuVar' => 'menu',
//            'authVar' => 'user',
//            'authModel' => 'User',
//            'authField' => 'group'
//        )
    );

    public function beforeFilter() {
        parent::beforeFilter();
        //$this->Auth->allow('*');
    }

    public function beforeRender() {
        parent::beforeRender();
        //$this->helpers = array_merge(parent::__get('helpers'), $this->helpers);
        // User
        if ($this->Auth->loggedIn()) {
            $user = $this->Auth->user();
            $this->set(compact('user'));
        }
        // Menu
        Configure::load('menu');
        $menu = Configure::read('menu');
        $this->set(compact('menu'));

//        // For Mobile
//        if ($this->request->is('mobile')) {
//            $this->set('client', 'mobile');
//        }
    }

    /**
     * 
     */
//    public function isAuthorized($user = null) {
//        if (isset($this->request->params['admin']) && !empty($this->request->params['admin'])) {
//            return (bool) ($user['group'] === 'admin');
//        }
//    }
}
